package com.zhang.zxx.boot.trans;

import com.github.repository.TransRepository;
import com.zhang.zxx.boot.entity.DO.SysDept;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * UserTransRepository:
 *
 * @author zhangxiaoxiang
 * @since 2025/01/23
 */
@Component
@RequiredArgsConstructor
public class DeptTransRepository implements TransRepository {

    @Resource
    private TransDriver transDriver;

    /**
     * 根据id集合查询对象集合
     * @param transValues 需要翻译的值
     * @param transAnno   翻译对象上的注解
     * @return 翻译后的值集合
     */

    @Override
    public Map<Object, Object> getTransValueMap(List<Object> transValues, Annotation transAnno) {
        if (transAnno instanceof DbTrans) {
            DbTrans dbTrans = (DbTrans) transAnno;
            List<SysDept> byIds = (List<SysDept>) transDriver.findByIds(getIds(transValues), dbTrans.entity());
            return byIds.stream().collect(Collectors.toMap(SysDept::getId, x -> x));
        }
        return Collections.emptyMap();
    }

    /**
     * 获取查询id
     */
    private List<String> getIds(List<Object> transValues) {
        System.out.println("transValues===>" + transValues);
        // 这里写关联逻辑
        return transValues.stream()
                .map(x -> (String) x)
                .collect(Collectors.toList());
    }
}
